<?php
/* Quak Bak 0.3 | http://quakbak.com
 
Copyright (c) 2009 Jeremy Hilton 
 
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>. */

////////////////////////////////////////
////////// Configuration Section ///////

// Cache On: TRUE turns the cache on. FALSE turns the cache off. It is recommended that you leave the cache on.
$cache_on = TRUE;

// Path to Cache File: This is the path to tb_cache.txt from the root of your server (/)
// If left blank, the shortener library will look for the cache file in the DOCUMENT_ROOT of your site
// EX: $path_to_cache_file = '/usr/local/apache/htdocs/yourblogsite/tb_cache.txt';
$path_to_cache_file = '';

///////// Do Not Edit Below Here //////
///////////////////////////////////////

// This PHP file handles the shortening of your blog post URL
function return_shortened_url($post_url,$service, $cache_enabled=FALSE, $cache_contents_lookup_array=FALSE)
{
	switch($service)
	{
		case 'tinyurl':
			if($cache_hit = check_cache($post_url,$service, $cache_enabled, $cache_contents_lookup_array))
			{		
				return $cache_hit;

			}

			$url = 'http://tinyurl.com/api-create.php?url=' . $post_url;
			
			return get_short_url($url, $post_url, 'tinyurl', $cache_enabled);

			break;

		case 'bitly':
			if($cache_hit = check_cache($post_url,$service, $cache_enabled, $cache_contents_lookup_array))
                        {
                                return $cache_hit;

                        }

			$url = 'http://bit.ly/api?url=' . $post_url;

			return get_short_url($url, $post_url, 'bitly', $cache_enabled);

			break;
		
		case 'isgd':
			if($cache_hit = check_cache($post_url,$service, $cache_enabled, $cache_contents_lookup_array))
                        {
                                return $cache_hit;

                        }

			$url = 'http://is.gd/api.php?longurl=' . $post_url;

			return get_short_url($url, $post_url, 'isgd', $cache_enabled);

			break;

	}

	$output = curl_exec($ch);

        curl_close($ch);

	return $output;

}

function get_short_url($url, $post_url, $service, $cache_enabled)
{
	$ch = curl_init();		
	curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1) ;	

	$output = curl_exec($ch);
        curl_close($ch);

	if($cache_enabled === TRUE)
	{
		$cache_string = $post_url . '|' . $service . '|' . $output; 

		// cache is enabled, write out our results
		$fp = @fopen($_SERVER['DOCUMENT_ROOT'] . '/tb_cache.txt', "a+");
		if(@flock($fp, LOCK_EX))
		{
			@fwrite($fp, $cache_string . "\n");

		}
		@flock($fp, LOCK_UN);

		@fclose($fp);

	}

        return $output;

}

function check_cache($post_url,$service, $cache_enabled, $cache_contents_lookup_array)
{
	if($cache_enabled === TRUE && $cache_contents_lookup_array !== FALSE)
        {
        	if($cache_contents_lookup_array[$post_url][$service])
        	{
        		return $cache_contents_lookup_array[$post_url][$service];

        	}

        }

}

function return_shortened_url_wrapper($post_url)
{
	global $cache_on;

	// is caching enabled?
        if($cache_on === TRUE)
        {
		// open our filecache, if it exists
        	// if it doesn't, no worries, caching will be disabled
        	if(file_exists($_SERVER['DOCUMENT_ROOT'] . '/tb_cache.txt')) 
        	{
			$cache_contents = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/tb_cache.txt');
	
			// make the flat cache content data into an array
			$c_content_temp1 = explode("\n", $cache_contents);

			if(is_array($c_content_temp1))
			{
				foreach($c_content_temp1 as $key => $value)
				{
					$c_content_temp2 = explode("|", $value);

					$cache_contents_lookup_array[$c_content_temp2[0]][$c_content_temp2[1]] = $c_content_temp2[2];
	
				}

		
			}

               		$cache_enabled = TRUE;

        	}

	}

	// put together JSON packet of results
	$ors_url = '{"urls": {';
	$ors_url .= '"tinyurl":"' . return_shortened_url($post_url, 'tinyurl' ,$cache_enabled, $cache_contents_lookup_array) . '",';
	$ors_url .= '"bitly":"' . return_shortened_url($post_url, 'bitly' ,$cache_enabled, $cache_contents_lookup_array) . '",';
	$ors_url .= '"isgd":"' . return_shortened_url($post_url, 'isgd' ,$cache_enabled, $cache_contents_lookup_array) . '"';
	// $ors_url .= '"isgd":"' . return_shortened_url($post_url, 'isgd' ,$cache_enabled, $cache_contents_lookup_array) . '",';
	// $ors_url .= '"post_url":"' . $post_url . '"';

	$ors_url .= '}}';

	return $ors_url;

}

// determine the path of the cache file
$tb_cache = ($path_to_cache_file == '') ? $_SERVER['DOCUMENT_ROOT'] . '/tb_cache.txt' : $path_to_cache_file;

if($_GET['url'])
{
	print return_shortened_url_wrapper($_GET['url']);

}
else
{
	print 'error';
}
?>
